package com.apexwear.apexutils.ui;

import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.apex.bluetooth.core.EABleManager;
import com.apex.bluetooth.enumeration.EABleConnectState;
import com.apex.bluetooth.model.EABleWeather;
import com.apexwear.apexutils.R;
import com.apexwear.apexutils.api.OnPositiveClickListener;
import com.apexwear.apexutils.data.WeatherData;
import com.apexwear.apexutils.databinding.ActivityTimeBinding;
import com.apexwear.apexutils.databinding.ActivityWeatherBinding;
import com.apexwear.apexutils.dialog.SelectDialog;
import com.apexwear.apexutils.utils.ShowToast;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class WeatherActivity extends AppCompatActivity {
    ActivityWeatherBinding dataBinding;
    String[] unitArray, currentTempArray, addressArray, dayWeatherArray, nightWeatherArray, airArray, raysArray, moonArray,
            minTempArray, maxTempArray, sunriseArray, sunsetArray, minWindArray, maxWindArray, humidityArray, cloudArray, qualityArray;
    SelectDialog unitDialog, currentTempDialog, addressDialog, dayDialog, nightDialog, airDialog, raysDialog, moonDialog,
            minTempDialog, maxTempDialog, sunriseDialog, sunsetDialog, minWindDialog, maxWindDialog, humidityDialog, cloudDialog, qualityDialog;
    WeatherData weatherData;
    int currentMinTemp, currentMaxTemp, currentMinWind, currentMaxWind, currentHumidity, currentCloud, currentTemp;
    String defaultUnit;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_weather);
        dataBinding.tooBar.setNavigationIcon(R.mipmap.exit_page);
        setSupportActionBar(dataBinding.tooBar);
        getSupportActionBar().setTitle(null);
        defaultUnit = getString(R.string.celsius);
        if (weatherData == null) {
            weatherData = new ViewModelProvider(WeatherActivity.this).get(WeatherData.class);
        }
        dataBinding.tooBar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        dataBinding.rowUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (unitArray == null) {
                    unitArray = getResources().getStringArray(R.array.temp_unit);
                }
                if (unitDialog == null) {
                    unitDialog = new SelectDialog(WeatherActivity.this, unitArray, null);
                    unitDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.unit.setText(mac);
                            if (defaultUnit.equalsIgnoreCase(mac)) {
                                return;
                            }
                            defaultUnit = mac;
                            if (mac.equalsIgnoreCase(getString(R.string.fahrenheit))) {
                                String cTemp = dataBinding.currentTemp.getText().toString();
                                if (!TextUtils.isEmpty(cTemp)) {
                                    currentTemp = Math.round(Float.valueOf(currentTemp) * 1.8F) + 32;
                                    dataBinding.currentTemp.setText(currentTemp + getString(R.string.f_unit));
                                }
                                String minTemp = dataBinding.minTemp.getText().toString();
                                if (!TextUtils.isEmpty(minTemp)) {
                                    currentMinTemp = Math.round(Float.valueOf(currentMinTemp) * 1.8F) + 32;
                                    dataBinding.minTemp.setText(currentMinTemp + getString(R.string.f_unit));
                                }
                                String maxTemp = dataBinding.maxTemp.getText().toString();
                                if (!TextUtils.isEmpty(maxTemp)) {
                                    currentMaxTemp = Math.round(Float.valueOf(currentMaxTemp) * 1.8F) + 32;
                                    dataBinding.maxTemp.setText(currentMaxTemp + getString(R.string.f_unit));
                                }

                            } else {
                                String cTemp = dataBinding.currentTemp.getText().toString();
                                if (!TextUtils.isEmpty(cTemp)) {
                                    currentTemp = Math.round((Float.valueOf(currentTemp) - 32) / 1.8F);
                                    dataBinding.currentTemp.setText(currentTemp + getString(R.string.c_unit));
                                }
                                String minTemp = dataBinding.minTemp.getText().toString();
                                if (!TextUtils.isEmpty(minTemp)) {
                                    currentMinTemp = Math.round((Float.valueOf(currentMinTemp) - 32) / 1.8F);
                                    dataBinding.minTemp.setText(currentMinTemp + getString(R.string.c_unit));
                                }
                                String maxTemp = dataBinding.maxTemp.getText().toString();
                                if (!TextUtils.isEmpty(maxTemp)) {
                                    currentMaxTemp = Math.round((Float.valueOf(currentMaxTemp) - 32) / 1.8F);
                                    dataBinding.maxTemp.setText(currentMaxTemp + getString(R.string.c_unit));
                                }
                            }
                        }
                    });
                }
                if (!unitDialog.isShowing()) {
                    unitDialog.show();
                }
            }
        });
        dataBinding.rowTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentTempArray == null) {
                    currentTempArray = new String[20];
                    for (int i = 0; i < 20; i++) {
                        currentTempArray[i] = (i - 120 + 10 * i) + "";
                    }
                }
                if (currentTempDialog == null) {
                    currentTempDialog = new SelectDialog(WeatherActivity.this, currentTempArray, null);
                    currentTempDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            String unit = dataBinding.unit.getText().toString();
                            if (!TextUtils.isEmpty(unit) && unit.equalsIgnoreCase(getString(R.string.fahrenheit))) {
                                currentTemp = Math.round(Float.valueOf(mac) * 1.8F) + 32;
                                dataBinding.currentTemp.setText(currentTemp + getString(R.string.f_unit));
                            } else {
                                currentTemp = Math.round(Float.valueOf(mac));
                                dataBinding.currentTemp.setText(mac + getString(R.string.c_unit));
                            }
                        }
                    });
                }
                if (!currentTempDialog.isShowing()) {
                    currentTempDialog.show();
                }
            }
        });
        dataBinding.rowAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (addressArray == null) {
                    addressArray = getResources().getStringArray(R.array.city_array);
                }
                if (addressDialog == null) {
                    addressDialog = new SelectDialog(WeatherActivity.this, addressArray, null);
                    addressDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.city.setText(mac);
                        }
                    });
                }
                if (!addressDialog.isShowing()) {
                    addressDialog.show();
                }
            }
        });
        dataBinding.rowDayState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dayWeatherArray == null) {
                    dayWeatherArray = getResources().getStringArray(R.array.weather_array);
                }
                if (dayDialog == null) {
                    dayDialog = new SelectDialog(WeatherActivity.this, dayWeatherArray, null);
                    dayDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.dayWeather.setText(mac);
                        }
                    });
                }
                if (!dayDialog.isShowing()) {
                    dayDialog.show();
                }
            }
        });
        dataBinding.rowNightState.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (nightWeatherArray == null) {
                    nightWeatherArray = getResources().getStringArray(R.array.weather_array);
                }
                if (nightDialog == null) {
                    nightDialog = new SelectDialog(WeatherActivity.this, nightWeatherArray, null);
                    nightDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.nightWeather.setText(mac);
                        }
                    });
                }
                if (!nightDialog.isShowing()) {
                    nightDialog.show();
                }
            }
        });
        dataBinding.rowAir.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (airArray == null) {
                    airArray = getResources().getStringArray(R.array.air_array);
                }
                if (airDialog == null) {
                    airDialog = new SelectDialog(WeatherActivity.this, airArray, null);
                    airDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.air.setText(mac);
                        }
                    });
                }
                if (!airDialog.isShowing()) {
                    airDialog.show();
                }
            }
        });
        dataBinding.rowRays.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (raysArray == null) {
                    raysArray = getResources().getStringArray(R.array.rays_array);
                }
                if (raysDialog == null) {
                    raysDialog = new SelectDialog(WeatherActivity.this, raysArray, null);
                    raysDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.rays.setText(mac);
                        }
                    });
                }
                if (!raysDialog.isShowing()) {
                    raysDialog.show();
                }
            }
        });
        dataBinding.rowMoon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (moonArray == null) {
                    moonArray = getResources().getStringArray(R.array.moon_array);
                }
                if (moonDialog == null) {
                    moonDialog = new SelectDialog(WeatherActivity.this, moonArray, null);
                    moonDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String mac) {
                            dataBinding.moon.setText(mac);
                        }
                    });
                }
                if (!moonDialog.isShowing()) {
                    moonDialog.show();
                }
            }
        });
        dataBinding.rowMinTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (minTempArray == null) {
                    minTempArray = new String[20];
                    for (int i = 0; i < 20; i++) {
                        minTempArray[i] = (i - 120 + 10 * i) + "";
                    }
                }
                if (minTempDialog == null) {
                    minTempDialog = new SelectDialog(WeatherActivity.this, minTempArray, null);
                    minTempDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            String unit = dataBinding.unit.getText().toString();
                            if (!TextUtils.isEmpty(unit) && unit.equalsIgnoreCase(getString(R.string.fahrenheit))) {
                                currentMinTemp = Math.round(Float.valueOf(o) * 1.8F) + 32;
                                dataBinding.minTemp.setText(currentMinTemp + getString(R.string.f_unit));
                            } else {
                                currentMinTemp = Math.round(Float.valueOf(o));
                                dataBinding.minTemp.setText(o + getString(R.string.c_unit));
                            }
                        }
                    });
                }
                if (!minTempDialog.isShowing()) {
                    minTempDialog.show();
                }
            }
        });
        dataBinding.rowMaxTemp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (maxTempArray == null) {
                    maxTempArray = new String[20];
                    for (int i = 0; i < 20; i++) {
                        maxTempArray[i] = (i - 120 + i * 10) + "";
                    }
                }
                if (maxTempDialog == null) {
                    maxTempDialog = new SelectDialog(WeatherActivity.this, maxTempArray, null);
                    maxTempDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            String unit = dataBinding.unit.getText().toString();
                            if (!TextUtils.isEmpty(unit) && unit.equalsIgnoreCase(getString(R.string.fahrenheit))) {
                                currentMaxTemp = Math.round(Float.valueOf(o) * 1.8F) + 32;
                                dataBinding.maxTemp.setText(currentMaxTemp + getString(R.string.f_unit));
                            } else {
                                currentMaxTemp = Math.round(Float.valueOf(o));
                                dataBinding.maxTemp.setText(o + getString(R.string.c_unit));
                            }
                        }
                    });
                }
                if (!maxTempDialog.isShowing()) {
                    maxTempDialog.show();
                }
            }
        });
        dataBinding.rowSunrise.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sunriseArray == null) {
                    sunriseArray = getResources().getStringArray(R.array.hour_array);
                }
                if (sunriseDialog == null) {
                    sunriseDialog = new SelectDialog(WeatherActivity.this, sunriseArray, null);
                    sunriseDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            dataBinding.sunrise.setText(o);
                        }
                    });
                }
                if (!sunriseDialog.isShowing()) {
                    sunriseDialog.show();
                }
            }
        });
        dataBinding.rowSunset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (sunsetArray == null) {
                    sunsetArray = getResources().getStringArray(R.array.hour_array);
                }
                if (sunsetDialog == null) {
                    sunsetDialog = new SelectDialog(WeatherActivity.this, sunsetArray, null);
                    sunsetDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            dataBinding.sunset.setText(o);
                        }
                    });
                }
                if (!sunsetDialog.isShowing()) {
                    sunsetDialog.show();
                }
            }
        });
        dataBinding.rowMinLevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (minWindArray == null) {
                    minWindArray = getResources().getStringArray(R.array.hour_array);
                }
                if (minWindDialog == null) {
                    minWindDialog = new SelectDialog(WeatherActivity.this, minWindArray, null);
                    minWindDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            currentMinWind = Math.round(Float.valueOf(o));
                            dataBinding.minLevel.setText(o + getString(R.string.wind_unit));
                        }
                    });
                }
                if (!minWindDialog.isShowing()) {
                    minWindDialog.show();
                }
            }
        });
        dataBinding.rowMaxLevel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (maxWindArray == null) {
                    maxWindArray = getResources().getStringArray(R.array.hour_array);
                }
                if (maxWindDialog == null) {
                    maxWindDialog = new SelectDialog(WeatherActivity.this, maxWindArray, null);
                    maxWindDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            currentMaxWind = Math.round(Float.valueOf(o));
                            dataBinding.maxLevel.setText(o + getString(R.string.wind_unit));
                        }
                    });
                }
                if (!maxWindDialog.isShowing()) {
                    maxWindDialog.show();
                }
            }
        });
        dataBinding.rowHumidity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (humidityArray == null) {
                    humidityArray = new String[21];
                    for (int i = 0; i < 21; i++) {
                        humidityArray[i] = (i * 5 + "");
                    }
                }
                if (humidityDialog == null) {
                    humidityDialog = new SelectDialog(WeatherActivity.this, humidityArray, null);
                    humidityDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            currentHumidity = Math.round(Float.valueOf(o));
                            dataBinding.humidity.setText(o + getString(R.string.percent_unit));
                        }
                    });
                }
                if (!humidityDialog.isShowing()) {
                    humidityDialog.show();
                }
            }
        });
        dataBinding.rowCloud.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cloudArray == null) {
                    cloudArray = new String[21];
                    for (int i = 0; i < 21; i++) {
                        cloudArray[i] = (i * 5 + "");
                    }
                }

                if (cloudDialog == null) {
                    cloudDialog = new SelectDialog(WeatherActivity.this, cloudArray, null);
                    cloudDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            currentCloud = Math.round(Float.valueOf(o));
                            dataBinding.cloud.setText(o + getString(R.string.percent_unit));
                        }
                    });
                }
                if (!cloudDialog.isShowing()) {
                    cloudDialog.show();
                }
            }
        });
        dataBinding.rowQuality.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (qualityArray == null) {
                    qualityArray = new String[21];
                    for (int i = 0; i < 21; i++) {
                        qualityArray[i] = (i * 5 + "");
                    }
                }

                if (qualityDialog == null) {
                    qualityDialog = new SelectDialog(WeatherActivity.this, qualityArray, null);
                    qualityDialog.setOnPositiveClickListener(new OnPositiveClickListener<String>() {
                        @Override
                        public void clickButton(String o) {
                            dataBinding.quality.setText(o);
                        }
                    });
                }
                if (!qualityDialog.isShowing()) {
                    qualityDialog.show();
                }
            }
        });
        dataBinding.sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String unit = dataBinding.unit.getText().toString();
                String cTemp = dataBinding.currentTemp.getText().toString();
                String address = dataBinding.city.getText().toString();
                String dayWeather = dataBinding.dayWeather.getText().toString();
                String nightWeather = dataBinding.nightWeather.getText().toString();
                String air = dataBinding.air.getText().toString();
                String rays = dataBinding.rays.getText().toString();
                String moon = dataBinding.moon.getText().toString();
                String sunriseTime = dataBinding.sunrise.getText().toString();
                String sunsetTime = dataBinding.sunset.getText().toString();
                String quality = dataBinding.quality.getText().toString();

                if (TextUtils.isEmpty(unit) || TextUtils.isEmpty(address) || TextUtils.isEmpty(dayWeather) || TextUtils.isEmpty(nightWeather) || TextUtils.isEmpty(cTemp)) {
                    ShowToast.show(WeatherActivity.this, getString(R.string.weather_hint));
                    return;
                }
                EABleWeather eaBleWeather = new EABleWeather();
                if (unit.equalsIgnoreCase(getString(R.string.celsius))) {
                    eaBleWeather.setTemperatureUnit(EABleWeather.TemperatureUnit.centigrade);
                } else {
                    eaBleWeather.setTemperatureUnit(EABleWeather.TemperatureUnit.fahrenheit);
                }
                eaBleWeather.setPlace(address);
                eaBleWeather.setCurrent_temperature(Float.valueOf(currentTemp));

                List<EABleWeather.EABleWeatherItem> itemList = new ArrayList<>();
                EABleWeather.EABleWeatherItem item = new EABleWeather.EABleWeatherItem();
                itemList.add(item);
                eaBleWeather.setS_day(itemList);
                item.setAir_humidity(currentHumidity);
                item.setCloudiness(currentCloud);
                item.setMax_temperature(currentMaxTemp);
                item.setMin_temperature(currentMinTemp);
                item.setMax_wind_power(currentMaxWind);
                item.setMin_wind_power(currentMinWind);
                if (!TextUtils.isEmpty(quality)) {
                    item.setAir_grade(Integer.valueOf(quality));
                }
                if (!TextUtils.isEmpty(sunriseTime)) {
                    Calendar calendar = Calendar.getInstance();
                    if (sunriseTime.contains(":")) {
                        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(sunriseTime.split(":")[0]));
                    } else {
                        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(sunriseTime));
                    }


                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    item.setSunrise_timestamp(calendar.getTimeInMillis() / 1000);
                }
                if (!TextUtils.isEmpty(sunsetTime)) {
                    Calendar calendar = Calendar.getInstance();
                    if (sunsetTime.contains(":")) {
                        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(sunsetTime.split(":")[0]));
                    } else {
                        calendar.set(Calendar.HOUR_OF_DAY, Integer.valueOf(sunsetTime));
                    }
                    calendar.set(Calendar.MINUTE, 0);
                    calendar.set(Calendar.SECOND, 0);
                    calendar.set(Calendar.MILLISECOND, 0);
                    item.setSunset_timestamp(calendar.getTimeInMillis() / 1000);
                }
                if (!TextUtils.isEmpty(air)) {
                    if (air.equalsIgnoreCase(getString(R.string.air_1))) {
                        item.setE_air(EABleWeather.AirQuality.excellent);
                    } else if (air.equalsIgnoreCase(getString(R.string.air_2))) {
                        item.setE_air(EABleWeather.AirQuality.good);
                    } else {
                        item.setE_air(EABleWeather.AirQuality.bad);
                    }
                }
                if (!TextUtils.isEmpty(rays)) {
                    if (rays.equalsIgnoreCase(getString(R.string.rays_1))) {
                        item.setE_rays(EABleWeather.RaysLevel.weak);
                    } else if (rays.equalsIgnoreCase(getString(R.string.rays_2))) {
                        item.setE_rays(EABleWeather.RaysLevel.medium);
                    } else if (rays.equalsIgnoreCase(getString(R.string.rays_3))) {
                        item.setE_rays(EABleWeather.RaysLevel.strong);
                    } else if (rays.equalsIgnoreCase(getString(R.string.rays_4))) {
                        item.setE_rays(EABleWeather.RaysLevel.very_strong);
                    } else if (rays.equalsIgnoreCase(getString(R.string.rays_5))) {
                        item.setE_rays(EABleWeather.RaysLevel.super_strong);
                    }
                }
                if (!TextUtils.isEmpty(moon)) {
                    if (moon.equalsIgnoreCase(getString(R.string.moon_1))) {
                        item.setE_moon(EABleWeather.Moon.new_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_2))) {
                        item.setE_moon(EABleWeather.Moon.waxing_crescent_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_3))) {
                        item.setE_moon(EABleWeather.Moon.quarter_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_4))) {
                        item.setE_moon(EABleWeather.Moon.half_moon_1);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_5))) {
                        item.setE_moon(EABleWeather.Moon.waxing_gibbous_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_6))) {
                        item.setE_moon(EABleWeather.Moon.full_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_7))) {
                        item.setE_moon(EABleWeather.Moon.waning_gibbous_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_8))) {
                        item.setE_moon(EABleWeather.Moon.half_moon_2);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_9))) {
                        item.setE_moon(EABleWeather.Moon.last_quarter_moon);
                    } else if (moon.equalsIgnoreCase(getString(R.string.moon_10))) {
                        item.setE_moon(EABleWeather.Moon.waning_crescent_moon);
                    }
                }
                if (dayWeather.equalsIgnoreCase(getString(R.string.weather_1))) {
                    item.setE_day_type(EABleWeather.WeatherType.clear);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_2))) {
                    item.setE_day_type(EABleWeather.WeatherType.cloudy);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_3))) {
                    item.setE_day_type(EABleWeather.WeatherType.gloomy);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_4))) {
                    item.setE_day_type(EABleWeather.WeatherType.drizzle);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_5))) {
                    item.setE_day_type(EABleWeather.WeatherType.moderate_rain);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_6))) {
                    item.setE_day_type(EABleWeather.WeatherType.thunderstorm);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_7))) {
                    item.setE_day_type(EABleWeather.WeatherType.heavy_rain);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_8))) {
                    item.setE_day_type(EABleWeather.WeatherType.sleet);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_9))) {
                    item.setE_day_type(EABleWeather.WeatherType.light_snow);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_10))) {
                    item.setE_day_type(EABleWeather.WeatherType.moderate_snow);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_11))) {
                    item.setE_day_type(EABleWeather.WeatherType.heavy_snow);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_12))) {
                    item.setE_day_type(EABleWeather.WeatherType.typhoon);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_13))) {
                    item.setE_day_type(EABleWeather.WeatherType.dust);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_14))) {
                    item.setE_day_type(EABleWeather.WeatherType.sandstorm);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_15))) {
                    item.setE_day_type(EABleWeather.WeatherType.fog);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_16))) {
                    item.setE_day_type(EABleWeather.WeatherType.haze);
                }
                if (nightWeather.equalsIgnoreCase(getString(R.string.weather_1))) {
                    item.setE_night_type(EABleWeather.WeatherType.clear);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_2))) {
                    item.setE_night_type(EABleWeather.WeatherType.cloudy);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_3))) {
                    item.setE_night_type(EABleWeather.WeatherType.gloomy);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_4))) {
                    item.setE_night_type(EABleWeather.WeatherType.drizzle);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_5))) {
                    item.setE_night_type(EABleWeather.WeatherType.moderate_rain);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_6))) {
                    item.setE_night_type(EABleWeather.WeatherType.thunderstorm);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_7))) {
                    item.setE_night_type(EABleWeather.WeatherType.heavy_rain);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_8))) {
                    item.setE_night_type(EABleWeather.WeatherType.sleet);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_9))) {
                    item.setE_night_type(EABleWeather.WeatherType.light_snow);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_10))) {
                    item.setE_night_type(EABleWeather.WeatherType.moderate_snow);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_11))) {
                    item.setE_night_type(EABleWeather.WeatherType.heavy_snow);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_12))) {
                    item.setE_night_type(EABleWeather.WeatherType.typhoon);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_13))) {
                    item.setE_night_type(EABleWeather.WeatherType.dust);
                } else if (nightWeather.equalsIgnoreCase(getString(R.string.weather_14))) {
                    item.setE_night_type(EABleWeather.WeatherType.sandstorm);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_15))) {
                    item.setE_night_type(EABleWeather.WeatherType.fog);
                } else if (dayWeather.equalsIgnoreCase(getString(R.string.weather_16))) {
                    item.setE_night_type(EABleWeather.WeatherType.haze);
                }
                Log.e("WeatherActivity", "发送出去的:" + new Gson().toJson(eaBleWeather));
                if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
                    weatherData.setWeather(eaBleWeather).observe(WeatherActivity.this, new Observer<Boolean>() {
                        @Override
                        public void onChanged(Boolean aBoolean) {
                            if (aBoolean) {
                                ShowToast.show(WeatherActivity.this, getString(R.string.add_success));
                            } else {
                                ShowToast.show(WeatherActivity.this, getString(R.string.add_fail));
                            }
                        }
                    });
                }

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (EABleManager.getInstance().getDeviceConnectState() == EABleConnectState.STATE_CONNECTED) {
            weatherData.getWeather().observe(WeatherActivity.this, new Observer<EABleWeather>() {
                @Override
                public void onChanged(EABleWeather eaBleWeather) {
                    if (eaBleWeather != null) {
                        Log.i("WeatherActivity", new Gson().toJson(eaBleWeather));

                        currentTemp = Math.round(eaBleWeather.getCurrent_temperature());
                        if (TextUtils.isEmpty(eaBleWeather.getPlace())) {
                            dataBinding.city.setText("广州");
                        } else {
                            dataBinding.city.setText(eaBleWeather.getPlace());
                        }
                        EABleWeather.TemperatureUnit temperatureUnit = eaBleWeather.getTemperatureUnit();
                        if (temperatureUnit != null && temperatureUnit == EABleWeather.TemperatureUnit.fahrenheit) {
                            dataBinding.unit.setText(getString(R.string.fahrenheit));
                            dataBinding.currentTemp.setText(eaBleWeather.getCurrent_temperature() + getString(R.string.f_unit));
                            defaultUnit = getString(R.string.fahrenheit);
                        } else {
                            dataBinding.unit.setText(getString(R.string.celsius));
                            defaultUnit = getString(R.string.celsius);
                            dataBinding.currentTemp.setText(eaBleWeather.getCurrent_temperature() + getString(R.string.c_unit));
                        }
                        List<EABleWeather.EABleWeatherItem> itemList = eaBleWeather.getS_day();
                        if (itemList != null && !itemList.isEmpty()) {
                            EABleWeather.EABleWeatherItem item = itemList.get(0);
                            currentHumidity = item.getAir_humidity();
                            dataBinding.humidity.setText(item.getAir_humidity() + getString(R.string.percent_unit));
                            currentCloud = item.getCloudiness();
                            dataBinding.cloud.setText(item.getCloudiness() + getString(R.string.percent_unit));
                            if (temperatureUnit != null && temperatureUnit == EABleWeather.TemperatureUnit.fahrenheit) {
                                dataBinding.maxTemp.setText(item.getMax_temperature() + getString(R.string.f_unit));
                                dataBinding.minTemp.setText(item.getMin_temperature() + getString(R.string.f_unit));
                            } else {
                                dataBinding.maxTemp.setText(item.getMax_temperature() + getString(R.string.c_unit));
                                dataBinding.minTemp.setText(item.getMin_temperature() + getString(R.string.c_unit));
                            }
                            currentMaxTemp = Math.round(Float.valueOf(item.getMax_temperature()));
                            currentMinTemp = Math.round(Float.valueOf(item.getMin_temperature()));
                            currentMaxWind = Math.round(Float.valueOf(item.getMax_wind_power()));
                            dataBinding.maxLevel.setText(item.getMax_wind_power() + getString(R.string.wind_unit));
                            currentMinWind = Math.round(Float.valueOf(item.getMin_wind_power()));
                            dataBinding.minLevel.setText(item.getMin_wind_power() + getString(R.string.wind_unit));
                            dataBinding.quality.setText(item.getAir_grade() + "");
                            long sunriseTime = item.getSunrise_timestamp();
                            Calendar calendar = Calendar.getInstance();
                            calendar.setTimeInMillis(sunriseTime * 1000);
                            dataBinding.sunrise.setText(calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
                            long sunsetTime = item.getSunset_timestamp();
                            calendar.clear();
                            calendar.setTimeInMillis(sunsetTime * 1000);
                            dataBinding.sunset.setText(calendar.get(Calendar.HOUR_OF_DAY) + ":" + calendar.get(Calendar.MINUTE));
                            EABleWeather.AirQuality airQuality = item.getE_air();
                            if (airQuality != null) {
                                if (airQuality == EABleWeather.AirQuality.bad) {
                                    dataBinding.air.setText(getString(R.string.air_3));
                                } else if (airQuality == EABleWeather.AirQuality.excellent) {
                                    dataBinding.air.setText(getString(R.string.air_1));
                                } else {
                                    dataBinding.air.setText(getString(R.string.air_2));
                                }
                            }
                            EABleWeather.Moon moon = item.getE_moon();
                            if (moon != null) {
                                if (moon == EABleWeather.Moon.new_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_1));
                                } else if (moon == EABleWeather.Moon.waxing_crescent_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_2));
                                } else if (moon == EABleWeather.Moon.quarter_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_3));
                                } else if (moon == EABleWeather.Moon.half_moon_1) {
                                    dataBinding.moon.setText(getString(R.string.moon_4));
                                } else if (moon == EABleWeather.Moon.waxing_gibbous_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_5));
                                } else if (moon == EABleWeather.Moon.full_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_6));
                                } else if (moon == EABleWeather.Moon.waning_gibbous_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_7));
                                } else if (moon == EABleWeather.Moon.half_moon_2) {
                                    dataBinding.moon.setText(getString(R.string.moon_8));
                                } else if (moon == EABleWeather.Moon.last_quarter_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_9));
                                } else if (moon == EABleWeather.Moon.waning_crescent_moon) {
                                    dataBinding.moon.setText(getString(R.string.moon_10));
                                }
                            }
                            EABleWeather.RaysLevel raysLevel = item.getE_rays();
                            if (raysLevel != null) {
                                if (raysLevel == EABleWeather.RaysLevel.weak) {
                                    dataBinding.rays.setText(getString(R.string.rays_1));
                                } else if (raysLevel == EABleWeather.RaysLevel.medium) {
                                    dataBinding.rays.setText(getString(R.string.rays_2));
                                } else if (raysLevel == EABleWeather.RaysLevel.strong) {
                                    dataBinding.rays.setText(getString(R.string.rays_3));
                                } else if (raysLevel == EABleWeather.RaysLevel.very_strong) {
                                    dataBinding.rays.setText(getString(R.string.rays_4));
                                } else if (raysLevel == EABleWeather.RaysLevel.super_strong) {
                                    dataBinding.rays.setText(getString(R.string.rays_5));
                                }
                            }
                            EABleWeather.WeatherType weatherType = item.getE_day_type();
                            if (weatherType != null) {
                                if (weatherType == EABleWeather.WeatherType.clear) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_1));
                                } else if (weatherType == EABleWeather.WeatherType.cloudy) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_2));
                                } else if (weatherType == EABleWeather.WeatherType.gloomy) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_3));
                                } else if (weatherType == EABleWeather.WeatherType.drizzle) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_4));
                                } else if (weatherType == EABleWeather.WeatherType.moderate_rain) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_5));
                                } else if (weatherType == EABleWeather.WeatherType.thunderstorm) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_6));
                                } else if (weatherType == EABleWeather.WeatherType.heavy_rain) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_7));
                                } else if (weatherType == EABleWeather.WeatherType.sleet) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_8));
                                } else if (weatherType == EABleWeather.WeatherType.light_snow) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_9));
                                } else if (weatherType == EABleWeather.WeatherType.moderate_snow) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_10));
                                } else if (weatherType == EABleWeather.WeatherType.heavy_snow) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_11));
                                } else if (weatherType == EABleWeather.WeatherType.typhoon) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_12));
                                } else if (weatherType == EABleWeather.WeatherType.dust) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_13));
                                } else if (weatherType == EABleWeather.WeatherType.sandstorm) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_14));
                                } else if (weatherType == EABleWeather.WeatherType.fog) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_15));
                                } else if (weatherType == EABleWeather.WeatherType.haze) {
                                    dataBinding.dayWeather.setText(getString(R.string.weather_16));
                                }
                            }
                            EABleWeather.WeatherType nightWeatherType = item.getE_night_type();
                            if (nightWeatherType != null) {
                                if (nightWeatherType == EABleWeather.WeatherType.clear) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_1));
                                } else if (nightWeatherType == EABleWeather.WeatherType.cloudy) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_2));
                                } else if (nightWeatherType == EABleWeather.WeatherType.gloomy) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_3));
                                } else if (nightWeatherType == EABleWeather.WeatherType.drizzle) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_4));
                                } else if (nightWeatherType == EABleWeather.WeatherType.moderate_rain) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_5));
                                } else if (nightWeatherType == EABleWeather.WeatherType.thunderstorm) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_6));
                                } else if (nightWeatherType == EABleWeather.WeatherType.heavy_rain) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_7));
                                } else if (nightWeatherType == EABleWeather.WeatherType.sleet) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_8));
                                } else if (nightWeatherType == EABleWeather.WeatherType.light_snow) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_9));
                                } else if (nightWeatherType == EABleWeather.WeatherType.moderate_snow) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_10));
                                } else if (nightWeatherType == EABleWeather.WeatherType.heavy_snow) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_11));
                                } else if (nightWeatherType == EABleWeather.WeatherType.typhoon) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_12));
                                } else if (nightWeatherType == EABleWeather.WeatherType.dust) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_13));
                                } else if (nightWeatherType == EABleWeather.WeatherType.sandstorm) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_14));
                                } else if (nightWeatherType == EABleWeather.WeatherType.fog) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_15));
                                } else if (nightWeatherType == EABleWeather.WeatherType.haze) {
                                    dataBinding.nightWeather.setText(getString(R.string.weather_16));
                                }
                            }
                        } else {
                            dataBinding.nightWeather.setText(getString(R.string.weather_1));
                            dataBinding.dayWeather.setText(getString(R.string.weather_1));
                            dataBinding.rays.setText(getString(R.string.rays_1));
                            dataBinding.moon.setText(getString(R.string.moon_1));
                            dataBinding.air.setText(getString(R.string.air_2));
                            dataBinding.sunset.setText("18");
                            dataBinding.sunrise.setText("6");
                            dataBinding.unit.setText(getString(R.string.celsius));
                            dataBinding.currentTemp.setText(20 + getString(R.string.c_unit));
                            dataBinding.city.setText("广州");
                            dataBinding.humidity.setText(5 + getString(R.string.percent_unit));
                            dataBinding.cloud.setText(10 + getString(R.string.percent_unit));
                            dataBinding.maxTemp.setText(25 + getString(R.string.c_unit));
                            dataBinding.minTemp.setText(15 + getString(R.string.c_unit));
                            dataBinding.maxLevel.setText(3 + getString(R.string.wind_unit));
                            dataBinding.minLevel.setText(1 + getString(R.string.wind_unit));
                            dataBinding.quality.setText("5");
                            currentTemp = 20;
                            defaultUnit = getString(R.string.celsius);
                            currentHumidity = 5;
                            currentCloud = 10;
                            currentMaxTemp = 25;
                            currentMinTemp = 15;
                            currentMaxWind = 3;
                            currentMinWind = 1;
                        }


                    }
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        if (unitDialog != null) {
            unitDialog.destroyDialog();
            unitDialog = null;
        }
        if (currentTempDialog != null) {
            currentTempDialog.destroyDialog();
            currentTempDialog = null;
        }
        if (addressDialog != null) {
            addressDialog.destroyDialog();
            addressDialog = null;
        }
        if (dayDialog != null) {
            dayDialog.destroyDialog();
            dayDialog = null;
        }
        if (nightDialog != null) {
            nightDialog.destroyDialog();
            nightDialog = null;
        }
        if (airDialog != null) {
            airDialog.destroyDialog();
            airDialog = null;
        }
        if (raysDialog != null) {
            raysDialog.destroyDialog();
            raysDialog = null;
        }
        if (moonDialog != null) {
            moonDialog.destroyDialog();
            moonDialog = null;
        }
        if (minTempDialog != null) {
            minTempDialog.destroyDialog();
            minTempDialog = null;
        }
        if (maxTempDialog != null) {
            maxTempDialog.destroyDialog();
            maxTempDialog = null;
        }
        if (sunriseDialog != null) {
            sunriseDialog.destroyDialog();
            sunriseDialog = null;
        }
        if (sunsetDialog != null) {
            sunsetDialog.destroyDialog();
            sunsetDialog = null;
        }
        if (minWindDialog != null) {
            minWindDialog.destroyDialog();
            minWindDialog = null;
        }
        if (maxWindDialog != null) {
            maxWindDialog.destroyDialog();
            maxWindDialog = null;
        }
        if (humidityDialog != null) {
            humidityDialog.destroyDialog();
            humidityDialog = null;
        }
        if (cloudDialog != null) {
            cloudDialog.destroyDialog();
            cloudDialog = null;
        }
        if (qualityDialog != null) {
            qualityDialog.destroyDialog();
            qualityDialog = null;
        }
        unitArray = null;
        currentTempArray = null;
        addressArray = null;
        dayWeatherArray = null;
        nightWeatherArray = null;
        airArray = null;
        raysArray = null;
        moonArray = null;
        minTempArray = null;
        maxTempArray = null;
        sunriseArray = null;
        sunsetArray = null;
        minWindArray = null;
        maxWindArray = null;
        humidityArray = null;
        cloudArray = null;
        qualityArray = null;
        weatherData = null;
        if (dataBinding != null) {
            dataBinding.unbind();
            dataBinding = null;
        }
        super.onDestroy();
    }
}
